﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.ModuleEdits;
using System.Linq;
using DevExpress.XtraTab;
using DevExpress.XtraRichEdit;
using ECRMS.WinForm.Modules.ReportPrintControl;
using ECRMS.WinForm.Modules.BaseRibbonControl;

namespace ECRMS.WinForm.Modules
{
    [ModuleInstance((int)ModuleEnum.报表填写)]
    public partial class ReportDesignModule : BaseModule
    {
        public List<RibbonModule> RichEdits = new List<RibbonModule>();

        public ReportDesignModule()
        {
            base.core.CreateWaitDialog("初始化中请稍候...","报表填写");
            InitializeComponent();
            InitView();
            InitData();;
            InitEventHandler();
            base.core.CloseWaitDialog();
        }

        private void InitEventHandler()
        {
            //语言切换
            base.core.LangChanged += new Core.LanguageChangedHandler(this.ChangeLanguage);
        }

        private void InitView()
        {
            base.newBarButtonVisiable = false;
            base.selectBarButtonVisiable = false;
            base.updateBarButtonVisiable = false;
            base.delBarButtonVisiable = false;
            base.selectBarButtonVisiable = false;
            base.otherBarButton1Visiable = false;
            base.otherBarButton2Visiable = false;
            base.otherBarButton3Visiable = false;
            if (core.CoreUser.UserInfo.UserTypeId == (int)AuthorityEnum.CorporationClientAdmin)
            {
                base.outBarButtonVisiable = true;
            }
            else
            {
                base.outBarButtonVisiable = false;
            }
        }

        private void InitData()
        {
            FuncResult result = core.CoreReportOfDesign.GetReportsByEmployee(core.CoreUser.UserInfo.CorporationRefId, core.CoreUser.UserInfo.UserRefId);
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }
            
            tabList.BeginUpdate();
            foreach (CtReport report in core.CoreReportOfDesign.Reports)
            {

                XtraTabPage newTab = new XtraTabPage()
                {
                    Text = report.Name,
                    Name = report.ReportPartRefId.ToString(),
                    Image = base.imageStore.Images[43]
                };
                RibbonModule control = new RibbonModule() {
                    Name = "rec" + report.ReportPartRefId.ToString(),
                    Dock = DockStyle.Fill,
                    ContentText = report.Content != null ? System.Text.Encoding.UTF8.GetString(report.Content.ToArray()) : ""
                };
                
                RichEdits.Add(control);
                newTab.Controls.Add(control);

                tabList.TabPages.Add(newTab);
            }
            tabList.EndUpdate();
        }

        public override void saveBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.saveBarButton_ItemClick(sender, e);
            DialogResult result = XtraMessageBox.Show(this, "确定保存修过的报表内容？", "报表内容", MessageBoxButtons.YesNo);
            
            if (result == DialogResult.Yes)
            {
                core.CreateWaitDialog("数据提交中，请稍后", "数据提交中", 150, 50, true, this);
               
                foreach (CtReport item in core.CoreReportOfDesign.Reports)
                {
                    RibbonModule control = this.Controls.Find("rec" + item.ReportPartRefId.ToString(), true)[0] as RibbonModule;
                    if (control != null)
                    {
                        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(control.ContentText);
                        //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(control.Text);
                        item.Content = buffer;
                    }
                }

                FuncResult result1 = core.CoreReportOfDesign.UpdateReports();
                if (result1.Status == 0)
                {
                    core.CloseWaitDialog();
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                    return;
                }
                core.CloseWaitDialog();
            }
        }

        public override void refreshBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.refreshBarButton_ItemClick(sender, e);
            InitData();
        }

        private void ChangeLanguage(LangEnum lang)
        {
            //switch (core.NowLanguage)
            switch (lang)
            {
                case LangEnum.China:
                case LangEnum.中文:
                   
                    break;
                case LangEnum.English:
                case LangEnum.英文:
                   
                    break;
                default:
                    break;
            }
        }

        public override void outBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.outBarButton_ItemClick(sender, e);
            //PrintMainForm printForm = new PrintMainForm();
            //DialogResult result = printForm.ShowDialog();
            ReportOutModule printForm = new ReportOutModule(this) ;
            DialogResult result = printForm.ShowDialog();
            printForm.Dispose();
        }
    }
}
